﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using COFE.Core.Actions;
using COFE.Core.Lister;
using COFE.Core.Entry;

namespace COFE.Data
{
    public class CacheEntryWork : WorkBase
    {
        #region Constructor

        private CacheEntryWork(Guid workId, IPropertyProvider primaryPropertyProvider)
            : base(workId)
        {
            Description = "Cache_Entry_" + primaryPropertyProvider.GetParseName();
            _entryId = primaryPropertyProvider.GetId();            
            _primaryPropertyProvider = primaryPropertyProvider;
        }

        private CacheEntryWork(Guid workId, IFileSystemInfo entry)
            : this(workId, entry.InternalProperties.PrimaryPropertyProvider)
        {        
        }

        private CacheEntryWork(Guid workId, Guid entryId, string fullParsePath)
            : base(workId)
        {
            Description = "Cache_Entry_" + fullParsePath;
            _entryId = entryId;
            _primaryPropertyProvider = null;
        }

        /// <summary>
        /// Schedule a CacheEntryWork in COFEAction.WorkScheduler 
        /// </summary>
        /// <param name="entryId"></param>
        /// <param name="primaryPropertyProvider"> </param>
        /// <returns>Schedule id</returns>
        public static Guid ScheduleWork(IPropertyProvider primaryPropertyProvider)
        {
            var work = new CacheEntryWork(Guid.NewGuid(), primaryPropertyProvider);
            return COFEActionShared.WorkScheduler.Enqueue(WorkPriority.Normal, work);
        }

        /// <summary>
        /// Schedule a CacheEntryWork in COFEAction.WorkScheduler.
        /// </summary>
        /// <param name="entryId"></param>
        /// <param name="entry"></param>
        /// <returns>Schedule id</returns>
        public static Guid ScheduleWork(IInternalFileSystemInfoExA entry)
        {
            var work = new CacheEntryWork(Guid.NewGuid(), entry);
            return COFEActionShared.WorkScheduler.Enqueue(WorkPriority.Normal, work);
        }

        /// <summary>
        /// Schedule a CacheEntryWork in COFEAction.WorkScheduler.
        /// </summary>
        /// <param name="entryId">The entry must already exists in database.</param>        
        /// <returns>Schedule id</returns>
        public static Guid ScheduleWork(Guid entryId, string fullParsePath)
        {
            var work = new CacheEntryWork(Guid.NewGuid(), entryId, fullParsePath);
            return COFEActionShared.WorkScheduler.Enqueue(WorkPriority.Normal, work);
        }

        #endregion

        #region Methods

        protected override void DoWork()
        {
            if (_primaryPropertyProvider == null)
            {
                var entry = COFEShared.CurrentUserProfile.TryParseGuid(_entryId, 
                    Core.Profile.EntryConstructionFlags.DefaultWithoutCache);
                if (entry != null)
                    _primaryPropertyProvider = entry.InternalProperties.PrimaryPropertyProvider;
            }

            if (_primaryPropertyProvider != null)
            {
                COFEDBShared.DBEntryManager.UpdateEntry(_primaryPropertyProvider);
                //Schedule Cache Properties too.
                CachePropertiesWork.ScheduleWork(_primaryPropertyProvider); 
            }
        }

        #endregion

        #region Data

        private Guid _entryId;
        private IPropertyProvider _primaryPropertyProvider;

        #endregion 

        #region Public Properties

        #endregion 
        
    }
}
